<?php
namespace Admin\Controller;
use Think\Controller;
class TypeController extends AdminController {
	// 显示并添加页面
    public function index(){
    	$this->assign('category', M('category')->select());
    	$this->display();
    }
    // 添加类型
    public function create(){
    	if(IS_POST){
    		$data['category_name'] = I('category_name');
    		$data['category_slug'] = I('category_slug');
    		if(M('category')->add($data)>0){
    			$this->success('添加成功','/admin/type/index');
    		}else{
    			$this->error('添加失败','/admin/type/index');
    		}
    	}else{
    		$this -> error('404 Not Found');
    	}
    }
    // 修改类型
    public function edit($id){
    	$cate = M('category')->where('id=%d',$id)->find();
    	$categorys = M('category')->select();
    	if(IS_POST){
    		$data['category_name'] = I('category_name');
    		$data['category_slug'] = I('category_slug');
    		if(M('category')->where('id=%d',$id)->save($data)>0){
    			$this->success('修改成功','/admin/type/index');
    		}else{
    			$this->error('修改失败','/admin/type/index');
    		}
    	}else{
    		$this->assign('categorys',$categorys);
    		$this->assign('cate',$cate);
    		$this->display();
    	}
    }
    // 删除类型
    public function delete($id){
    	if(IS_POST){
    		if(M('category')->where('id=%d',$id)->delete()>0){
    			$this->success('删除成功');
    		}else{
    			$this->error('删除失败');
    		}
    	}else{
    		$this -> error('404 Not found');
    	}
    }

}